﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using PccCommonForC;

public partial class Pub_Module_Login : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtUserName.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnLogin.ClientID + "').click();return false;}} else {return true}; ");
            txtPassword.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnLogin.ClientID + "').click();return false;}} else {return true}; ");   
            StateLogin();
        }
    }

    private void StateLogin()
    {
        if (Session["UserID"] != null && Session["IsAdmin"]=="N")
        {
            pnlLogin.Visible = false;
            pnlLogout.Visible = true;
            lblUserNm.Text = Session["UserName"].ToString();
        }
        else
        {
            pnlLogin.Visible = true;
            pnlLogout.Visible = false;
            lblUserNm.Text = "";
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        clsCommon clsCom = new clsCommon();

        RijndaelManaged mobjCryptoService = new RijndaelManaged();
        EncrypKey clsEncryp = new EncrypKey(mobjCryptoService);

        string UserNm = txtUserName.Text.Trim();
        string Password = clsEncryp.Encrypting(txtPassword.Text, System.Configuration.ConfigurationManager.AppSettings["EncrypKey"]);

        clsWebShop cls = new clsWebShop();
        DataSet ds = new DataSet();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("UserNm", cls.killSQLInjectionLogin(UserNm));
        msg.CreateFirstNode("Password", cls.killSQLInjectionLogin(Password));
        msg.CreateFirstNode("SuperAdmin", System.Configuration.ConfigurationManager.AppSettings["SuperAdmin"]);
        msg.CreateFirstNode("SupperPass", System.Configuration.ConfigurationManager.AppSettings["SuperPass"]);

        ds = cls.Get_InfLoginCustomerCondition(msg.GetXmlStr.ToString());
        DataTable dtExist = ds.Tables["TExist"];
        if (dtExist.Rows.Count > 0)
        {
            if (dtExist.Rows[0]["Exist"].ToString() == "Y")
            {
                DataTable dtUser = new DataTable();
                DataTable dtMenu = new DataTable();
                dtUser = ds.Tables[0];

                Session["UserID"] = dtUser.Rows[0]["UserID"].ToString();
                Session["UserName"] = dtUser.Rows[0]["UserNm"].ToString();
                Session["CusNm"] = dtUser.Rows[0]["CusNm"].ToString();
                Session["UserEmail"] = dtUser.Rows[0]["UserID"].ToString();
                Session["AutID"] = dtUser.Rows[0]["AutID"].ToString();    
                Session["IsAdmin"] = "N";
                Session["ErrLogin"] = "N";

                Response.Redirect("~/Member/Register/Success.aspx?pagefile=" + Request.QueryString["pagefile"]);
                //Session["UserPWD"] = dtUser.Rows[0]["UserID"].ToString();
            }
            else
            {
                Session["ErrLogin"] = "Y";
                Response.Redirect("~/Member/Login/Login.aspx");
            }

            //else if (dtExist.Rows[0]["Exist"].ToString() == "S")
            //{
            //    DataTable dtUser = new DataTable();
            //    DataTable dtMenu = new DataTable();
            //    dtUser = ds.Tables[0];
            //    dtMenu = ds.Tables[1];
            //    Session["UserID"] = dtUser.Rows[0]["UserID"].ToString();
            //    Session["UserName"] = dtUser.Rows[0]["UserNm"].ToString();
            //    Session["UserEmail"] = System.Configuration.ConfigurationManager.AppSettings["SuperPass"];
            //    Session["AutID"] = dtUser.Rows[0]["AutID"].ToString();
            //    Session["AuthTable"] = dtMenu;
            //    Session["IsAdmin"] = "S";
            //    //Session["UserPWD"] = dtUser.Rows[0]["UserID"].ToString();
            //    Response.Redirect("ManagerHome.aspx");
            //}
            //ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Đăng nhập không thành công')</script>");


        }
    }
    protected void lbtnSignout_Click(object sender, EventArgs e)
    {
        Session["UserID"] = null;
        StateLogin();
        Response.Redirect(Request.UrlReferrer.AbsoluteUri.ToString());
    }
}
